//
//  DropMenuButton.m
//  TengyunStation
//
//  Created by Johnny on 15/8/27.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "DropMenuButton.h"
#import "TSHeader.h"
#define CellHeight      44.0

@implementation DropMenuButton

- (id)initWithMenus:(NSArray *)menus InView:(UIView *)view {
    self = [super initWithFrame:CGRectMake(0, 0, 100, 30)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _menus = menus;
        NSString *title = [menus firstObject];
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 75, 20)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textAlignment = NSTextAlignmentRight;
        _nameLabel.textColor = OrangeColor;
        _nameLabel.text = title;
        [self addSubview:_nameLabel];
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(75, 7.5, 15, 15)];
        _imageView.image = [UIImage imageNamed:@"menu_down"];
        [self addSubview:_imageView];
        
        if (!_bgView) {
            _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
            _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView:)];
            tap.numberOfTapsRequired = 1;
            tap.delegate = self;
            [_bgView addGestureRecognizer:tap];
        }
        if (!_menuTableView) {
            _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(160, 0, 160, 0)];
            _menuTableView.dataSource = self;
            _menuTableView.delegate = self;
            _menuTableView.scrollEnabled = NO;
            _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            [_bgView addSubview:_menuTableView];
//            [_menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionBottom];
        }
        _bgSuperView = view;
        [self addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:_bgView];
    if (location.x >= 160 && location.y <= (CellHeight * _menus.count)) {
        return NO;
    }
    return YES;
}

- (void)hideBgView:(UITapGestureRecognizer *)recognizer {
    [self buttonAction];
}

- (void)buttonAction {
    if (_isShow) {
        _isShow = NO;
        [UIView animateWithDuration:0.2 animations:^{
            _menuTableView.frame = CGRectMake(_menuTableView.frame.origin.x, _menuTableView.frame.origin.y, _menuTableView.frame.size.width, 0);
            _bgView.alpha = 0;
        } completion:^(BOOL finished) {
            _imageView.image = [UIImage imageNamed:@"menu_down"];
            [_bgView removeFromSuperview];
        }];
    } else {
        _isShow = YES;
        _imageView.image = [UIImage imageNamed:@"menu_up"];
        [_bgSuperView addSubview:_bgView];
        [UIView animateWithDuration:0.2 animations:^{
            _bgView.alpha = 1;
            _menuTableView.frame = CGRectMake(_menuTableView.frame.origin.x, _menuTableView.frame.origin.y, _menuTableView.frame.size.width, CellHeight * _menus.count);
        }];
    }
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _menus.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"MenuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 7.7, 160, 20)];
        nameLabel.tag = 9;
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textColor = MainTextColor;
//        nameLabel.highlightedTextColor = OrangeColor;
        [cell.contentView addSubview:nameLabel];
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, (CellHeight - 0.5), 160, 0.5)];
        line.backgroundColor = LineColor;
        [cell.contentView addSubview:line];
        cell.backgroundView.backgroundColor = UIColorFromRGB(0xf5f5f5);
    }
    NSString *title = [_menus objectAtIndex:indexPath.row];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:9];
    nameLabel.text = title;
    if ([title isEqualToString:_nameLabel.text]) {
        nameLabel.textColor = OrangeColor;
    } else {
        nameLabel.textColor = MainTextColor;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self buttonAction];
    [tableView reloadData];
    NSString *title = [_menus objectAtIndex:indexPath.row];
    if (![title isEqualToString:_nameLabel.text]) {
        _nameLabel.text = title;
        if ([_delegate respondsToSelector:@selector(clickedButtonWithSearchType:)]) {
            [_delegate clickedButtonWithSearchType:[self searchTypeWithName:title]];
        }
    }
}

- (NSString *)searchTypeWithName:(NSString *)name {
    if ([name isEqualToString:@"全部"]) {
        return @"00";
    } else if ([name isEqualToString:@"待自提"]) {
        return @"01";
    } else if ([name isEqualToString:@"待配送"]) {
        return @"02";
    } else if ([name isEqualToString:@"已逾期"]) {
        return @"03";
    } else if ([name isEqualToString:@"已退件"]) {
        return @"04";
    } else if ([name isEqualToString:@"客户签收"]) {
        return @"05";
    } else if ([name isEqualToString:@"小站代签"]) {
        return @"06";
    } else if ([name isEqualToString:@"客户自签"]) {
        return @"07";
    }
    return @"";
}

- (void)dealloc {
    _menus = nil;
}

@end
